##################################################
## Authors: Stephanie L. DeMora, Christian Lindke, Sean Long, Jennifer L. Merolla, and Maricruz Ariana Osorio
## Project: Emotional Engagement in the Year of the Woman
## Purpose: Replication File for Publication: Appendix Tables and Figures
## Date: August 19, 2022
##################################################

# Load in all packages
Packages <- c("here", "car", "survey", "margins", "interplot","aod","dplyr","foreign","nnet","ggplot2","reshape2","Rmisc", "ggpubr",
              "tidyverse", "broom","dotwhisker","janitor","qualtRics","jtools","mediation","gtools","gridExtra", "huxtable", "DT", "ggsci", "hrbrthemes")

lapply(Packages, library, character.only = TRUE)

# GRAYSCALE Plot Theme:
steph_theme_gray <- list(
  hrbrthemes::theme_ipsum_rc(base_size = 12,
                             grid= "",
                             plot_title_size = 20,axis_text_size = 18),
  ggsci::scale_color_material("grey"),
  ggsci::scale_fill_material("grey"),
  theme(legend.position = "top",
        plot.caption = element_text(hjust = 0, face= "italic"), #Default is hjust=1
        plot.title.position = "plot", #NEW parameter. Apply for subtitle too.
        plot.caption.position =  "plot"))

# Read in data:
df <- readRDS("Qualtrics_2019.RDS")
df$TREATMENT_sort <- factor(df$TREATMENT, levels = c("Alexa","Trump","Clinton"))
df %>%
  filter(TREATMENT == "Trump" | TREATMENT == "Clinton" | TREATMENT == "Alexa") -> df

df_TESS <- readRDS("NORC.RDS")
df_TESS$TREATMENT_sort <- factor(df_TESS$TREATMENT, levels = c("Alexa","Trump","Clinton"))
df_TESS %>%
  filter(TREATMENT == "Trump" | TREATMENT == "Clinton" | TREATMENT == "Alexa") -> df_TESS

df_cces <- readRDS("CES2020.RDS")

# ---------------------------------------------------------------------------------------------------------------
# Table A1. Demographic Characteristics for Study 1 (Qualtrics)
# ---------------------------------------------------------------------------------------------------------------
df %>%
  tabyl(GENDER, sort = TRUE) %>%
  adorn_pct_formatting()


df %>%
  tabyl(RACE, sort = TRUE) %>%
  adorn_pct_formatting()


df %>%
  tabyl(AGE, sort = TRUE) %>%
  adorn_pct_formatting()

df %>%
  tabyl(PARTISANSHIP, sort = TRUE) %>%
  adorn_pct_formatting()

df %>%
  tabyl(EDUCATION, sort = TRUE)

# ---------------------------------------------------------------------------------------------------------------
# Table A2: Balance Checks, Qualtrics Panel, Study 1
# ---------------------------------------------------------------------------------------------------------------
regression <- multinom(TREATMENT ~ GENDER + RACE + AGE + party_leanersinc, data = df) # Run regression
summary <- summary(regression)$coefficients/summary(regression)$standard.errors #store summary
p <- (1 - pnorm(abs(summary), 0, 1)) * 2 #two tail z test
p

# ---------------------------------------------------------------------------------------------------------------
# Table A3. Demographic Characteristics for Study 2 (NORC panel)
# ---------------------------------------------------------------------------------------------------------------
df_TESS %>%
  tabyl(AGE7, sort = TRUE) %>%
  adorn_pct_formatting()

df_TESS %>%
  tabyl(GENDER, sort = TRUE) %>%
  adorn_pct_formatting()


df_TESS %>%
  tabyl(RACETHNICITY, sort = TRUE) %>%
  adorn_pct_formatting()

df %>%
  tabyl(PARTISANSHIP, sort = TRUE) %>%
  adorn_pct_formatting()

df_TESS %>%
  tabyl(EDUC, sort = TRUE) %>%
  adorn_pct_formatting()

# ---------------------------------------------------------------------------------------------------------------
# Table A4. Balance Checks, NORC Panel, Study 2
# ---------------------------------------------------------------------------------------------------------------
df_TESS$TREATMENT <- as.factor(as.character(df_TESS$TREATMENT))
regression <- multinom(TREATMENT ~ AGE + party, data = df_TESS) # Run regression
summary <- summary(regression)$coefficients/summary(regression)$standard.errors #store summary
p <- (1 - pnorm(abs(summary), 0, 1)) * 2 #two tail z test
p

# ---------------------------------------------------------------------------------------------------------------
# Table A5: Regressing the Experimental Conditions on Anger, Qualtrics Panel, Study 1
# ---------------------------------------------------------------------------------------------------------------
anger1 <- lm(anger ~ TREATMENT, data = df)
anger2 <- lm(anger ~ TREATMENT + party_leanersinc, data = df)
anger3 <- lm(anger ~ TREATMENT * party_leanersinc, data = df)
anger4 <- lm(anger ~ TREATMENT*GENDER, data = df)
anger5 <- lm(anger ~ TREATMENT*party_leanersinc, data = df, subset = df$GENDER == 1)
anger6 <- lm(anger ~ TREATMENT*EDUCATION_r, data = df, subset = df$GENDER == 1)
anger7 <- lm(anger ~ TREATMENT*GENDER*EDUCATION_r, data = df)
export_summs(anger1,anger2,anger3,anger4,anger5,anger6,anger7,model.names = c("None","Balanced","Party","Gender","Gender-Party","Gender-Edu", "Gender:Edu"))


# ---------------------------------------------------------------------------------------------------------------
# Table A6: Anger OLS Full Model Results, NORC Panel, Study 2
# ---------------------------------------------------------------------------------------------------------------
anger1 <- lm(anger ~ TREATMENT, data=df_TESS, na.action = na.omit,subset = party != "")
anger2 <- lm(anger ~ TREATMENT*party, data=df_TESS, na.action = na.omit)
export_summs(anger1,anger2, ci_level = 0.90, error_format = "({std.error})")


# ---------------------------------------------------------------------------------------------------------------
#  Table A7: Regressing the Experimental Conditions on Enthusiasm, Qualtrics Panel, Study 1
# ---------------------------------------------------------------------------------------------------------------

hope1 <- lm(hope ~ TREATMENT, data = df)
hope2 <- lm(hope ~ TREATMENT + party_leanersinc, data = df)
hope3 <- lm(hope ~ TREATMENT * party_leanersinc, data = df)
hope4 <- lm(hope ~ TREATMENT*GENDER, data = df)
hope5 <- lm(hope ~ TREATMENT*party_leanersinc, data = df, subset = df$GENDER == 1)
hope6 <- lm(hope ~ TREATMENT*EDUCATION_r, data = df, subset = df$GENDER == 1)
export_summs(hope1,hope2,hope3,hope4,hope5,hope6,model.names = c("None","Balanced","Party","Gender","Gender-Party","Gender-Edu"))


# ---------------------------------------------------------------------------------------------------------------
# Table A8: Mediation analysis with anger on full-scale for political engagement, Study 1 and Study 2
# ---------------------------------------------------------------------------------------------------------------

# Note: These may differ slightly each time they are run.

options(scipen=999)

## STUDY 1--
set.seed(20241130)
df1 <- subset(df,df$anger != 'NA'); df1 <- subset(df1,df1$fear != 'NA'); df1 <- subset(df1,df1$hope != 'NA')
model.0 <- lm(futureengage ~ TREATMENT, df1)
model.M <- lm(anger ~ TREATMENT, df1)
model.Y <- lm(futureengage ~ TREATMENT + anger + fear + hope, df1)
results <- mediate(model.M, model.Y, treat='TREATMENT', mediator='anger', boot=TRUE, sims=1000, conf.level = .90, control.value = "Alexa", treat.value = "Clinton")
# Clinton:
summary(results)
results <- mediate(model.M, model.Y, treat='TREATMENT', mediator='anger', boot=TRUE, sims=1000, conf.level = .90, control.value = "Alexa", treat.value = "Trump")
# Trump
summary(results)

## STUDY 2--
model.0 <- lm(futureengage ~ TREATMENT, df_TESS)
model.M <- lm(anger ~ TREATMENT, df_TESS)
model.Y <- lm(futureengage ~ TREATMENT + anger + fear + hope, df_TESS)
results <- mediate(model.M, model.Y, treat='TREATMENT', mediator='anger', boot=TRUE, sims=1000, conf.level = .90, control.value = "Alexa", treat.value = "Clinton")
#Clinton:
summary(results)
results <- mediate(model.M, model.Y, treat='TREATMENT', mediator='anger', boot=TRUE, sims=1000, conf.level = .90, control.value = "Alexa", treat.value = "Trump")
#Trump:
summary(results)


# ---------------------------------------------------------------------------------------------------------------
# Table A9: Enthusiasm OLS Full Model Results, NORC Panel, Study 2
# ---------------------------------------------------------------------------------------------------------------
model1 <- lm(hope ~ TREATMENT, data=df_TESS, na.action = na.omit,subset = party != "")
model2 <- lm(hope ~ TREATMENT*party, data=df_TESS, na.action = na.omit)
export_summs(model1,model2, ci_level = 0.90, error_format = "({std.error})")

# ---------------------------------------------------------------------------------------------------------------
# Table A10: Mediation analysis with enthusiasm on full-scale for political engagement
# ---------------------------------------------------------------------------------------------------------------
## STUDY 1--
model.0 <- lm(futureengage ~ TREATMENT, df1)
model.M <- lm(hope ~ TREATMENT, df1)
model.Y <- lm(futureengage ~ TREATMENT + hope + fear + anger, df1)
results <- mediate(model.M, model.Y, treat='TREATMENT', mediator='hope', boot=TRUE, sims=1000, conf.level = .90, control.value = "Alexa", treat.value = "Clinton")
# Clinton:
summary(results)
results <- mediate(model.M, model.Y, treat='TREATMENT', mediator='hope', boot=TRUE, sims=1000, conf.level = .90, control.value = "Alexa", treat.value = "Trump")
# Trump
summary(results)

## STUDY 2--
model.0 <- lm(futureengage ~ TREATMENT, df_TESS)
model.M <- lm(hope ~ TREATMENT, df_TESS)
model.Y <- lm(futureengage ~ TREATMENT + hope + anger + fear, df_TESS)
results <- mediate(model.M, model.Y, treat='TREATMENT', mediator='hope', boot=TRUE, sims=1000, conf.level = .90, control.value = "Alexa", treat.value = "Clinton")
# Clinton:
summary(results)
results <- mediate(model.M, model.Y, treat='TREATMENT', mediator='hope', boot=TRUE, sims=1000, conf.level = .90, control.value = "Alexa", treat.value = "Trump")
# Trump:
summary(results)

# ---------------------------------------------------------------------------------------------------------------
# Table A11. Fear OLS Full Model Results, Qualtrics Panel, Study 1
# ---------------------------------------------------------------------------------------------------------------
fear1 <- lm(fear ~ TREATMENT, data = df)
fear2 <- lm(fear ~ TREATMENT + party_leanersinc, data = df)
fear3 <- lm(fear ~ TREATMENT * party_leanersinc, data = df)
fear4 <- lm(fear ~ TREATMENT*GENDER, data = df)
fear5 <- lm(fear ~ TREATMENT*party_leanersinc, data = df, subset = df$GENDER == 1)
fear6 <- lm(fear ~ TREATMENT*EDUCATION_r, data = df, subset = df$GENDER == 1)
export_summs(fear1,fear2,fear3,fear4,fear5,fear6,model.names = c("None","Balanced","Party","Gender","Gender-Party","Gender-Edu"))

# ---------------------------------------------------------------------------------------------------------------
# Table A12. Marginal Effects of the Treatment on Anger and Fear among Women by Party, Qualtrics Panel, Study 1
# ---------------------------------------------------------------------------------------------------------------
# calculate margins at 90% instead of 95%
margins_summary <- function(model, ..., level = 0.90, by_factor = TRUE) {
  mar <- margins(model, ...)
  summary(mar, level = 0.90, by_factor = by_factor)
}

df1 <- subset(df, df$party_leanersinc != ""); df1 <- subset(df1, df1$GENDER == 1)
anger <- lm(anger ~ TREATMENT*party_leanersinc, data = df1)
margins_summary(anger, at = list(party_leanersinc = range(df1$party_leanersinc)))[3:6,]

df1 <- subset(df, df$party_leanersinc != ""); df1 <- subset(df1, df1$GENDER == 1)
fear <- lm(fear ~ TREATMENT*party_leanersinc, data = df1)
margins_summary(fear, at = list(party_leanersinc = range(df1$party_leanersinc)))[3:6,]

# ---------------------------------------------------------------------------------------------------------------
# Table A13: Fear OLS Full Model Results Study 2
# ---------------------------------------------------------------------------------------------------------------
model1 <- lm(fear ~ TREATMENT, data=df_TESS, na.action = na.omit,subset = party != "")
model2 <- lm(fear ~ TREATMENT*party, data=df_TESS, na.action = na.omit)
export_summs(model1,model2, ci_level = 0.90, error_format = "({std.error})")

# ---------------------------------------------------------------------------------------------------------------
# Table A14. Marginal Effects of the Treatment on Anger and Fear among Women by Party, NORC Panel, Study 2
# ---------------------------------------------------------------------------------------------------------------
df_TESS$TREATMENT <- as.factor(as.character(df_TESS$TREATMENT)); df_TESS1 <- subset(df_TESS, df_TESS$party != "")
anger <- lm(anger ~ TREATMENT*party, data = df_TESS1)
margins_summary(anger, at = list(party = range(df_TESS1$party)))[3:6,]

fear <- lm(fear ~ TREATMENT*party, data = df_TESS1)
margins_summary(fear, at = list(party = range(df_TESS1$party)))[3:6,]

# ---------------------------------------------------------------------------------------------------------------
# Table A15: Mediation analysis with fear on full-scale for political engagement
# ---------------------------------------------------------------------------------------------------------------
## STUDY 1--
df1 <- subset(df,df$anger != 'NA'); df1 <- subset(df1,df1$fear != 'NA'); df1 <- subset(df1,df1$hope != 'NA')
model.0 <- lm(futureengage ~ TREATMENT, df1)
model.M <- lm(fear ~ TREATMENT, df1)
model.Y <- lm(futureengage ~ TREATMENT + fear + hope + anger, df1)
results <- mediate(model.M, model.Y, treat='TREATMENT', mediator='fear', boot=TRUE, sims=1000, conf.level = .90, control.value = "Alexa", treat.value = "Clinton")
# Clinton:
summary(results)
results <- mediate(model.M, model.Y, treat='TREATMENT', mediator='fear', boot=TRUE, sims=1000, conf.level = .90, control.value = "Alexa", treat.value = "Trump")
# Trump:
summary(results)

## STUDY 2--
model.0 <- lm(futureengage ~ TREATMENT, df_TESS)
model.M <- lm(fear ~ TREATMENT, df_TESS)
model.Y <- lm(futureengage ~ TREATMENT + fear + anger + hope, df_TESS)
results <- mediate(model.M, model.Y, treat='TREATMENT', mediator='fear', boot=TRUE, sims=1000, conf.level = .90, control.value = "Alexa", treat.value = "Clinton")
# Clinton:
summary(results)
results <- mediate(model.M, model.Y, treat='TREATMENT', mediator='fear', boot=TRUE, sims=1000, conf.level = .90, control.value = "Alexa", treat.value = "Trump")
# Trump:
summary(results)

# ---------------------------------------------------------------------------------------------------------------
# Table A16: Anger OLS Full Model Results, NORC Panel, Study 2 with controls
# ---------------------------------------------------------------------------------------------------------------
mod1 <- lm(anger ~ TREATMENT + AGE + INCOME + IDEO, data = df_TESS)
mod2 <- lm(anger ~ TREATMENT * party + AGE+ INCOME + IDEO, data = df_TESS)
export_summs(mod1,mod2)

# ---------------------------------------------------------------------------------------------------------------
# Table A17: Enthusiasm OLS Full Model Results, NORC Panel, Study 2 with controls
# ---------------------------------------------------------------------------------------------------------------
mod1 <- lm(hope ~ TREATMENT + AGE + INCOME + IDEO, data = df_TESS)
mod2 <- lm(hope ~ TREATMENT * party + AGE+ INCOME + IDEO, data = df_TESS)
export_summs(mod1,mod2)

# ---------------------------------------------------------------------------------------------------------------
# Table A18: Fear OLS Full Model Results Study 2 with controls
# ---------------------------------------------------------------------------------------------------------------
mod1 <- lm(fear ~ TREATMENT + AGE + INCOME + IDEO, data = df_TESS)
mod2 <- lm(fear ~ TREATMENT * party + AGE+ INCOME + IDEO, data = df_TESS)
export_summs(mod1,mod2)

# ---------------------------------------------------------------------------------------------------------------
# Table A19: Anger OLS Full Model Results, Qualtrics Panel, Study 1 with controls
# ---------------------------------------------------------------------------------------------------------------
df$Age <- car::recode(df$AGE, "'18 - 24' = 1; '25 - 34' = 2; '35 - 44' = 3; '45 - 54' = 4; '55 - 64' = 5; '65 - 74' = 6; '75 - 84' = 7; '85 or older' = 8")
df$Income <- car::recode(df$INCOME, "'Less than $10,000' = 1; '$10,000 - $19,999' = 2; '$20,000 - $29,999' = 3; '$30,000 - $39,999' = 4; '$40,000 - $49,999' = 5; ' $50,000 - $59,999' = 6; '$60,000 - $69,999' = 7; '$70,000 - $79,999' = 8; '$80,000 - $89,999' = 9; '$90,000 - $99,999' = 10; '$100,000 - $149,999' = 11; '$150,000 or more' = 12; else = NA")
df$Ideology <- car::recode(df$IDEOLOGY, "'Extremely liberal' = 1; 'Liberal' = 2; 'Slightly liberal' = 3; 'Slightly conservative'; = 5; 'Conservative' = 6; 'Extremely Conservative' = 7; \"Haven't thought much about this\" = NA; else = 4")
mod1 <- lm(anger ~ TREATMENT + Age + Income + Ideology, data = df)
mod2 <- lm(anger ~ TREATMENT * party + Age + Income + Ideology, data = df)
mod3 <- lm(anger ~ TREATMENT + party + Age + Income + Ideology + Vote_Choice, data = df)
export_summs(mod1,mod2, mod3)

# ---------------------------------------------------------------------------------------------------------------
# Table A20: Fear OLS Full Model Results, Qualtrics Panel, Study 1 with controls
# ---------------------------------------------------------------------------------------------------------------
mod1 <- lm(fear ~ TREATMENT + Age + Income + Ideology, data = df)
mod2 <- lm(fear ~ TREATMENT * party + Age + Income + Ideology, data = df)
mod3 <- lm(fear ~ TREATMENT + party + Age + Income + Ideology + Vote_Choice, data = df)
export_summs(mod1,mod2, mod3)

# ---------------------------------------------------------------------------------------------------------------
# Table A21: Enthusiasm OLS Full Model Results, Qualtrics Panel, Study 1 with controls
# ---------------------------------------------------------------------------------------------------------------
mod1 <- lm(hope ~ TREATMENT + Age + Income + Ideology, data = df)
mod2 <- lm(hope ~ TREATMENT * party + Age + Income + Ideology, data = df)
mod3 <- lm(hope ~ TREATMENT + party + Age + Income + Ideology + Vote_Choice, data = df)
export_summs(mod1,mod2, mod3)

# ---------------------------------------------------------------------------------------------------------------
# Figure B1: Study 2 Average of Single Item within Enthusiasm Scale (Enthusiasm)
# ---------------------------------------------------------------------------------------------------------------
df_TESS1 <- subset(df_TESS,df_TESS$M1_1 != 'NA')
df_TESS1$TREATMENT_sort <- car::recode(df_TESS1$TREATMENT_sort, "'Alexa' = 'Control'")
df_TESS1$TREATMENT_sort <- factor(df_TESS1$TREATMENT_sort, levels = c("Trump","Clinton","Control"))
tgc <- summarySE(df_TESS1, measurevar="M1_1", groupvars=c("TREATMENT_sort"), conf.interval = .90)

mod1 <- lm(M1_1~TREATMENT_sort,data=df_TESS1)
test <- tidy(mod1,conf.int = TRUE,conf.level = 0.90)
tgc$low = tgc$M1_1-tgc$ci; tgc$high = tgc$M1_1+tgc$ci

ggplot(data=tgc, aes(x=TREATMENT_sort, y=M1_1)) +
  geom_bar(position = "dodge", stat="identity") +
  geom_errorbar(position = "dodge",aes(ymin=low, ymax=high)) + 
  steph_theme_gray + 
  labs(title="Mean Levels of Enthusiasm", 
       x="Treatment Condition", y = "Average level of Enthusiasm (single item)", caption = "Fall 2019 NORC Survey",subtitle = "By Experimental Condition", fill = "Gender:") + 
  theme(legend.position = "none")

# ---------------------------------------------------------------------------------------------------------------
# Figure B2: Study 2 Average of Single Item within Enthusiasm Scale (Hopeful)
# ---------------------------------------------------------------------------------------------------------------
df_TESS1 <- subset(df_TESS,df_TESS$M1_4 != 'NA')
df_TESS1$TREATMENT_sort <- car::recode(df_TESS1$TREATMENT_sort, "'Alexa' = 'Control'")
df_TESS1$TREATMENT_sort <- factor(df_TESS1$TREATMENT_sort, levels = c("Trump","Clinton","Control"))
tgc <- summarySE(df_TESS1, measurevar="M1_4", groupvars=c("TREATMENT_sort"), conf.interval = .90)
mod1 <- lm(M1_4~TREATMENT_sort,data=df_TESS1)
test <- tidy(mod1,conf.int = TRUE,conf.level = 0.90)
tgc$low = tgc$M1_4-tgc$ci; tgc$high = tgc$M1_4+tgc$ci

ggplot(data=tgc, aes(x=TREATMENT_sort, y=M1_4)) +
  geom_bar(position = "dodge", stat="identity") +
  geom_errorbar(position = "dodge",aes(ymin=low, ymax=high)) + 
  steph_theme_gray + 
  labs(title="Mean Levels of Hopeful", 
       x="Treatment Condition", y = "Average level of Hopeful (single item)", caption = "Fall 2019 NORC Survey",subtitle = "By Experimental Condition", fill = "Gender:") + 
  theme(legend.position = "none")

# ---------------------------------------------------------------------------------------------------------------
# Figure B3. Mean level of Fear by Experimental Condition, Study 1 & Study 2
# ---------------------------------------------------------------------------------------------------------------
df_TESS1 <- subset(df_TESS,df_TESS$fear != 'NA')
tgc <- summarySE(df_TESS1, measurevar="fear", groupvars=c("TREATMENT"), conf.interval = .90)
tgc$low = tgc$fear-tgc$ci; tgc$high = tgc$fear+tgc$ci; tgc$sort <- c(3,2,1)
tgc %>%
  arrange(sort) -> tgc
tgc$TREATMENT <- c("Trump","Clinton","Control")

enthus2_plot <- ggplot(data=tgc, aes(x=reorder(TREATMENT, sort), y=fear)) +
  geom_bar(position = "dodge", stat="identity") +
  geom_errorbar(position = "dodge",aes(ymin=low, ymax=high)) + 
  steph_theme_gray + 
  labs(title="", 
       x="Treatment Condition", y = "", caption = "Fall 2019 NORC Survey",subtitle = "", fill = "Gender:") + 
  theme(legend.position = "none")

summary(lm(fear ~ TREATMENT, df))
df1 <- subset(df,df$fear != "<NA>")
tgc <- summarySE(df1, measurevar="fear", groupvars=c("TREATMENT"), conf.interval = .90)
tgc$low = tgc$fear-tgc$ci; tgc$high = tgc$fear+tgc$ci; tgc$sort <- c(3,2,1)
tgc %>%
  arrange(sort) -> tgc
tgc$TREATMENT <- c("Trump","Clinton","Control")

enthus1_plot <- ggplot(data=tgc, aes(x=reorder(TREATMENT, sort), y=fear)) +
  geom_bar(position = "dodge", stat="identity") +
  geom_errorbar(position = "dodge",aes(ymin=low, ymax=high)) + 
  steph_theme_gray + 
  labs(title="Mean Levels of Fear", 
       x="Treatment Condition", y = "Average level of fear", caption = "Fall 2019 Qualtrics Survey",subtitle = "By Experimental Condition", fill = "Gender:") + 
  theme(legend.position = "none")

ggpubr::ggarrange(enthus1_plot, enthus2_plot, ncol = 2)

# ---------------------------------------------------------------------------------------------------------------
# Figure B4. Predicted values of treatment compliance by gender and partisanship
# ---------------------------------------------------------------------------------------------------------------
steph_theme <- list(
  hrbrthemes::theme_ipsum_rc(base_size = 12,plot_title_size = 20,axis_text_size = 18),
  theme(plot.caption = element_text(hjust = 0, face= "italic"),
        plot.title.position = "plot",
        plot.caption.position =  "plot"))


mod4 <- lm(Compliance ~ TREATMENT * Gender * Party, data = df_cces, weights = teamweight)
emm <- emmeans::emmeans(mod4, ~ c(TREATMENT, Gender, Party)) 
emm
emm %>%
  subset(TREATMENT == "Treatment of Women") -> emm
pairs(emm, adjust = "none")


mydf <- ggeffects::ggpredict(mod4, terms = c("Gender", "Party", 'TREATMENT'))
plot(mydf, connect.lines = F, ci_style = "errorbar", colors = "bw") +
  steph_theme +
  labs(title="Predicted Values of Compliance", y = "Compliance", caption = "CCES 2020",subtitle = "By Experimental Condition")

mod4 <- lm(word_count ~ TREATMENT * Gender * Party, data = df_cces, weights = teamweight)
emm <- emmeans::emmeans(mod4, ~ c(TREATMENT, Gender, Party)) 
emm
emm %>%
  subset(TREATMENT == "Treatment of Women") -> emm
pairs(emm, adjust = "none")

mydf <- ggeffects::ggpredict(mod4, terms = c("Gender", "Party", 'TREATMENT'))
plot(mydf, connect_lines = F, ci_style = "errorbar", colors = "bw") +
  steph_theme +
  labs(title="Predicted Values of Response Length", y = "Word Count", caption = "CCES 2020",subtitle = "By Experimental Condition")



# Analyses with Probability Weights:

des <- svydesign(
  ids = ~1,             # no PSUs provided
  weights = ~teamweight,    # general-population weight
  data = df_cces
)

mod4 <- svyglm(Compliance ~ TREATMENT * Gender * Party, design = des)
emm <- emmeans::emmeans(mod4, ~ c(TREATMENT, Gender, Party)) 
emm
emm %>%
  subset(TREATMENT == "Treatment of Women") -> emm
pairs(emm, adjust = "none")


mod4 <- svyglm(word_count ~ TREATMENT * Gender * Party, design = des)
emm <- emmeans::emmeans(mod4, ~ c(TREATMENT, Gender, Party)) 
emm
emm %>%
  subset(TREATMENT == "Treatment of Women") -> emm
pairs(emm, adjust = "none")


